<?php
	include_once("common.php");
	include_once("script/login.php");
	include_once("script/search.php");

	if(isVisitor() && isset($_SESSION['username']))
		unset($_SESSION['username']);

	if(isset($_POST['username']) &&
		isset($_POST['password'])){
		$logged = login($_POST['username'], $_POST['password']);
	}

	if(isset($_POST['logout'])){
		session_destroy();
		header("Location: index.php");
	}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>COMP 353 Project</title>

    <!-- Bootstrap core CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="./css/template.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy this line! -->
    <!--[if lt IE 9]><script src="../../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
</head>
<body>
	<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
		<div class="container">
			<div class="navbar-header">
				<a class="brand" href="#">SEMS Lite</a>
			</div>
			<div class="navbar">
				<ul class="nav navbar-nav">
					<li><a href="index.php">Home</a></li>
					<li class="active"><a href="search.php">Search</a></li>
					<li><a href="signup.php">Signup</a></li>
				</ul>
<?php
	if(isVisitor()){
		echo '<form class="navbar-form pull-right" method="post" action="index.php">'."\n",
			'<input name="username" type="text" placeholder="Username" class="form-control">'."\n",
			'<input name="password" type="password" placeholder="Password" class="form-control">'."\n",
			'<button type="submit" class="btn">Sign in</button>'."\n",
			'</form>'."\n";
	}
	else{
		echo '<form class="navbar-form pull-right" method="post" action="index.php">'."\n",
			'<input type="submit" class="btn" name="logout" value="Logout">'."\n",
			'</form>'."\n";
	}
?>
			</div>
		</div>
	</div>
<!-- CONTENT -->
<?php
	# Administrator menu
	if(isset($_SESSION['username']) && isAdmin($_SESSION['username']))
		include("script/admin_menu.php");

	if(!isVisitor())
		include_once("script/submenu.php");
?>
    
    <!-- Main content/tabs -->
	<div class="container">
		<form class="form-horizontal" action="search.php" method="get">
			<legend>Search</legend>
			<div class="control-group">
				<label class="control-label" for="inputKeyword">Keyword</label>
				<div class="controls">
					<input type="text" id="inputKeyword" placeholder="Keyword" name="keyword">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputTitle">Search In Title</label>
				<div class="controls">
					<input type="checkbox" id="inputTitle" name="searchTitle">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputContent">Search In Content</label>
				<div class="controls">
					<input type="checkbox" id="inputContent" name="searchContent">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputAfterTime">Post Made After</label>
				<div class="controls">
					<input type="datetime" placeholder="yyyy-mm-dd" id="inputAfterTime" name="afterTime">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputBeforeTime">Post Made Before</label>
				<div class="controls">
					<input type="datetime" placeholder="yyyy-mm-dd" id="inputBeforeTime" name="beforeTime">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputAuthorId">Post Made By Author ID</label>
				<div class="controls">
					<input type="number" id="inputAuthorId" placeholder="Author ID" name="authorID">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputAuthorName">Post Made By Author Name</label>
				<div class="controls">
					<input type="text" id="inputAuthorName" placeholder="Author Name" name="authorName">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputEventID">Post Made By Event ID</label>
				<div class="controls">
					<input type="number" id="inputEventId" placeholder="Event ID" name="eventID">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="inputEventName">Post Made By Event Name</label>
				<div class="controls">
					<input type="text" id="inputEventName" placeholder="Event Name" name="eventName">
				</div>
			</div>
			<div class="control-group">
				<div class="controls">
					<button type="submit" class="btn btn-primary">Search</button>
				</div>
			</div>
		</form>
    </div>
    
    <div class="container">
<?php
	if(sizeof($_GET) > 0)
	{
		// Convert checkboxes to booleans
		$searchTitle = (strtolower($_GET['searchTitle']) == 'on');
		$searchContent = (strtolower($_GET['searchContent']) == 'on');
		
		$p = searchPosts($_GET['keyword'], $searchTitle, $searchContent, 
					$_GET['afterTime'], $_GET['beforeTime'], 
					$_GET['authorID'], $_GET['authorName'], 
					$_GET['eventID'], $_GET['eventName']);

			if($p != false)
			{
				foreach($p as $post)
				{
						$container_class = "post";
						if($post['events_idevents'] !== null)
								$container_class .= " private";
						echo '<div class="'.$container_class.'">'."\n",
								'<h2>'.$post['title'].'</h2>'."\n",
								'<span class="author">'.$post['author']."@".$post['time'].'</span'."\n",
								'<p>'.$post['content'].'</p>'."\n",
								"</div>\n";
				}
			}
	}
?>
	</div>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
	<!-- CDN -->
	<!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> -->
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
	<!-- LOCAL -->
    <script src="./js/jquery-1.10.2.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
  </body>
</html>
